<?php
/**
 * The Template for displaying product archives, including the main shop page which is a post type archive.
 *
 * Override this template by copying it to yourtheme/woocommerce/archive-product.php
 *
 * @author 		WooThemes
 * @package 	WooCommerce/Templates
 * @version     2.0.0
 */
if (!defined('ABSPATH'))
    exit; // Exit if accessed directly

get_header('shop');
?>
<script type="text/javascript">
    $('.header').addClass('product shop-online');
</script>
<div class="row">
    <div class="col-lg-4 col-sm-4 col-12 header-description align-right">
        <div class="title step">
            <h2 class="line_1"><?php woocommerce_page_title(); ?></h2>

        </div>
    </div>
</div>
</div>
</div>
<div class="main-content container">
    <div class="row">
        <?php
        wp_reset_query();
        global $wp_query, $product;
		if(is_shop()){
        $curr_query = $wp_query->query_vars;
        $args = array_merge($wp_query->query_vars, array('posts_per_page' => '6', 'paged' => $curr_query['page'], 'orderby' => 'ID', 'order' => 'DESC'));
		}else{
		        $args = array_merge($wp_query->query_vars, array('posts_per_page' => '6', 'orderby' => 'ID', 'order' => 'DESC'));

		}
        query_posts($args);

        if (have_posts()) :
            $i = 0;
            while (have_posts()) : the_post();

                if ($i == 0) {
                    $post_id = get_the_ID();
                    $get_regular_price = get_post_meta($post_id, '_regular_price', true);
                    $get_regular_price = $get_regular_price ? $get_regular_price : 0;
                    $get_sku = get_post_meta($post_id, '_sku', true);
                    $tags = get_the_term_list($post_id, 'product_tag', '', ', ', '');
                    ?>
                    <div id="xprodetail" class="prodetail">

                        <div class=" col-lg-3 col-sm-4 col-12   hidden-sm">
                            <h3 class="contact-name"><?php echo get_the_title().' '.$get_sku ?></h3>
                            <div class="note"><?php the_content() ?></div>
                            <p>Tags: <?php echo $tags; ?></p>
                            <p id="prosize">
                                <span class="size active" itemid="s" >S</span>
                                <span class="size" itemid="m" >M</span>
                                <span class="size" itemid="l" >L</span>
                                <span class="size xl" itemid="xl" >XL</span>
                            </p>
                            <p class="price">
                                <span class="pull-left price-right"><?php echo number_format($get_regular_price, 0, ',', '.'); ?> VNĐ</span>
                                <span class="pull-right hidden-md"><?php echo $get_sku; ?></span>
                            </p>
                            <div>
                                <div class="col-lg-5 col-sm-5 product-cart no-right">
                                    <a href="<?php echo site_url() ?>/cart">Giỏ hàng (<span id="numpro"><?php echo $woocommerce->cart->cart_contents_count; ?></span>)</a>
                                </div>
                                <div class="col-lg-7 no-right ycart">
                                    <?php do_action('woocommerce_single_product_summary') ?>
                                </div>
                            </div>
                            <div class="clearfix"></div>

                            <div class="descpro"><?php do_action('woocommerce_single_product_excerpt') ?></div>

                            <div class="clearfix"></div>
                        </div>

                        <div class="col-lg-5 col-sm-8 col-12 m10 hidden-sm">
                            <div class="thumb-product m10">
                                <a href="<?php the_permalink() ?>">
                                    <?php //the_post_thumbnail(array(356, 520), array('class' => 'img-thumbnail')) ?>
                                    <img src="<?php echo get_mthumb_link($get_sku, '356_520'); ?>" class="img-thumbnail" />
                                </a>
                            </div>
                            <div class="col-lg-9 col-sm-8 no-left"><h4><?php the_title() ?></h4></div>
                            <div class="col-lg-3 col-sm-4 no-left">
                                <div class="btn btn-custom"><a href="<?php the_permalink() ?>">Chi tiết</a></div>
                            </div>
                        </div>
                    </div>
                    <div class="col-lg-4 col-sm-4 col-12 m10 list-product hidden-md">

                        <?php
                    }
                    echo!$i || !($i % 2) ? '<div class="row">' : '';
                    woocommerce_get_template_part('content', 'product');
                    echo!(($i + 1) % 2) ? '</div>' : '';
                    $i++;
                endwhile; // end of the loop.  
                echo (($i) % 2) ? '</div>' : '';
                ?>
                <!-- pagination --> 
                <div class="paginations">
                    <?php do_action('woocommerce_pagination') //echo woo_pagination($args);   ?>
                </div>
            </div>
        <?php elseif (!woocommerce_product_subcategories(array('before' => woocommerce_product_loop_start(false), 'after' => woocommerce_product_loop_end(false)))) : ?>

            <?php woocommerce_get_template('loop/no-products-found.php'); ?>

        <?php endif; ?>



    </div>
</div>
<!-- Content -->
<div class="container">
    <div class="row">
        <?php
        wp_reset_query();
        global $wp_query, $product;
        $curr_query = $wp_query->query_vars;
        ;
        $args = array_merge($wp_query->query_vars, array('posts_per_page' => '6', 'paged' => $curr_query['page'], 'orderby' => 'post_title', 'order' => 'DESC'));
        query_posts($args);

        if (have_posts()) :
            ?>
            <div class=" col-12 m10 list-product visible-md">
                <?php
                $i = 0;
                while (have_posts()) : the_post();
                    ?>


                    <?php
                    echo!$i || !($i % 3) ? '<div class="row">' : '';
                    woocommerce_get_template_part('content', 'product');
                    echo!(($i + 1) % 3) ? '</div>' : '';
                    $i++;
                endwhile; // end of the loop.  
                echo (($i ) % 3) ? '</div>' : '';
                ?>
                <!-- pagination --> 
                <div class="paginations">
                    <?php do_action('woocommerce_pagination') //echo woo_pagination($args);    ?>
                </div>
            </div>
        <?php elseif (!woocommerce_product_subcategories(array('before' => woocommerce_product_loop_start(false), 'after' => woocommerce_product_loop_end(false)))) : ?>

            <?php woocommerce_get_template('loop/no-products-found.php'); ?>

        <?php endif; ?>
    </div>
    <div class="row">
        <?php
        wp_reset_query();
        $posts_per_page = 6;
        query_posts(array('posts_per_page' => $posts_per_page));
        if (have_posts())
            while (have_posts()): the_post();
                if (has_post_thumbnail()) {
                    $large_image_url = wp_get_attachment_image_src(get_post_thumbnail_id(), 'large');
                    $url_to_image = $large_image_url[0];
                } else {
                    $url_to_image = bloginfo('template_url') . '/img/no-image-blog-one.png';
                }
                ?>
                <div class="col-lg-4 col-sm-6 col-12">
                    <div class="news-item">
                        <div class="news-thumb">
                              <a href="<?php the_permalink() ?>">
                            <img src="<?php echo $url_to_image ?>" />
                              </a>
                        </div>
                        <div class="news-title"><a href="<?php the_permalink() ?>"><?php the_title() ?></a></div>
                        <div class="news-description">
                            <?php the_excerpt(''); ?> 
                        </div>
                        <div class="read-more">
                            <a href="<?php the_permalink() ?>">&raquo; Chi tiết</a>
                        </div>

                    </div>
                </div>
            <?php endwhile; ?>

    </div>
</div>
<script type="text/javascript">

    jQuery('.prothumb').click(function() {
        $.blockUI({css: {border: 'none', padding: '15px', backgroundColor: '#000', '-webkit-border-radius': '10px', '-moz-border-radius': '10px', opacity: .5, color: '#fff'}});
        pro_id = jQuery(this).attr('ele');
        jQuery.post('<?php echo site_url() ?>/prodetail', {pro_id: pro_id}, function($data) {
            var $data = $data.replace("/prodetail?", "/shop/?");
            jQuery('#xprodetail').html($data);

            jQuery('form.variations_form').wc_variation_form();
            jQuery('form.variations_form .variations select').change();
            console.log(jQuery('#prosize .size'));

            jQuery('#prosize .size').on('click', function() {
                var is_same_choose = false;
                for (var i = 0; i < $('.size').length; i++) {
                    if ($(this).attr('itemid') == $($('.size')[i]).attr('itemid') && $($('.size')[i]).hasClass('active'))
                        is_same_choose = true;
                }
                $('.size').removeClass('active');
                if (is_same_choose == false)
                    $(this).addClass('active');
                eleval = jQuery(this).attr('itemid');
                jQuery('#pa_size option').each(function() {
                    console.log(eleval + ' ' + jQuery(this).val());
                    if (jQuery(this).val() === eleval) {
                        jQuery("#pa_size option").removeAttr('selected');
                        jQuery(this).attr('selected', 'selected');
                    }
                });
                jQuery("#pa_size").change();
            });
            jQuery.unblockUI();
        });
    });

    jQuery('#prosize .size').click(function() {
        jQuery('#pa_size').val(jQuery(this).attr('itemid'))
    });

</script>
<?php get_footer('shop'); ?>